<template>
    <div class="login-container">
      <!-- 登录表单 -->
      <form @submit.prevent="handleLogin">
        <h1>校园食堂代买餐平台</h1>
        <div class="input-group">
          <label for="card">校园卡号:</label>
          <input type="text" id="card" v-model="card" required/>
        </div>
        <div class="input-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="password" required />
        </div>
        <button type="submit">登录</button>
        <router-link to="/register" class="register-link">还没有账号？前往注册</router-link>
      </form>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import { useRouter } from 'vue-router';
  import axios from 'axios'; // 确保已经导入了axios 
  
  const card = ref('');
  const password = ref('');
  const router = useRouter();
  
  const handleLogin = () => {
    // 登录逻辑
    console.log('Logging in with:', { card, password });
    if(card!=' '&& password.value !=''){
      submitForm();
    }
    else if(card==' '){
      alert('请输入校园卡号');
    }else{
      alert('请输入密码');
    }
  };

  const submitForm = () => {
    const formData = new URLSearchParams();
    formData.append('card', card.value);
    formData.append('password', password.value);
    axios.post('http://localhost:8080/login', formData)
      .then(response => {
        console.log(response.data);
        if (response.data.success) {
          alert('登录成功！');
          localStorage.setItem('userID', card.value); // 保存token到本地存储
          router.push('/dashboard'); // 跳转到登录页面
        } else { 
          if(response.data.black == true){
            alert('该用户已被拉黑，请联系管理员');
            router.push('/user-login'); // 跳转到登录页面
          }else{
            alert('登录失败，请检查卡号或密码');
          }
        }
      })
      .catch(error => {
        console.error('There was an error!', error);
        alert('登录失败！');
      });
  };
  </script>
  
<style scoped>
  .login-container {
    max-width: 400px;
    margin: 100px auto;
    padding: 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    text-align: center;
  }
  
  h1 {
    color: #333;
    margin-bottom: 20px;
  }
  
  .input-group {
    margin-bottom: 15px;
  }
  
  input[type="text"],
  input[type="password"] {
    width: 100%;
    padding: 10px;
    margin-top: 5px;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-sizing: border-box; /* 使 padding 不影响宽度 */
  }
  
  button {
    width: 100%;
    padding: 10px;
    border: none;
    border-radius: 4px;
    background-color: #5cb85c; /* 绿色按钮 */
    color: white;
    font-size: 16px;
    cursor: pointer;
    margin-top: 10px;
  }
  
  button:hover {
    background-color: #4cae4c; /* 鼠标悬停时的按钮颜色 */
  }
  
  .register-link {
    display: inline-block;
    margin-top: 20px;
    color: #5cb85c;
    text-decoration: none;
    font-size: 14px;
  }
  
  .register-link:hover {
    text-decoration: underline;
  }
</style>